"""
反馈驱动的强化学习服务
将用户反馈转化为强化学习的奖励信号，持续优化系统决策
"""

import time
from typing import Dict, Any, List
from src.services.reinforcement_learning_service import get_reinforcement_learning_service
from src.services.mcp_feedback_enhanced_service import mcp_feedback_enhanced_service
from src.utils.logging import get_logger

logger = get_logger(__name__)

class FeedbackDrivenRLService:
    """反馈驱动的强化学习服务"""
    
    def __init__(self):
        self.rl_service = get_reinforcement_learning_service()
        self.feedback_service = mcp_feedback_enhanced_service
        self.feedback_to_reward_mapping = {
            "performance_positive": 5.0,
            "performance_negative": -3.0,
            "error_report": -10.0,
            "suggestion_positive": 2.0,
            "general_positive": 1.0,
            "general_negative": -1.0,
            "security_concern": -15.0
        }
    
    def convert_feedback_to_reward(self, feedback: Dict[str, Any]) -> float:
        """
        将用户反馈转换为奖励值
        
        Args:
            feedback: 用户反馈数据
            
        Returns:
            float: 奖励值
        """
        feedback_type = feedback.get("feedback_type", "general")
        priority = feedback.get("priority", "medium")
        processed = feedback.get("processed", False)
        
        # 基础奖励值
        base_reward = self.feedback_to_reward_mapping.get(f"{feedback_type}_positive", 0.0)
        if base_reward == 0.0:
            base_reward = self.feedback_to_reward_mapping.get(f"{feedback_type}_negative", 0.0)
            
        # 根据优先级调整奖励
        priority_multiplier = {
            "low": 0.5,
            "medium": 1.0,
            "high": 2.0,
            "critical": 3.0
        }.get(priority, 1.0)
        
        # 如果反馈已被处理，降低其奖励权重
        if processed:
            priority_multiplier *= 0.5
            
        return base_reward * priority_multiplier
    
    def process_feedback_batch(self, server_name: str) -> bool:
        """
        批量处理反馈并更新强化学习模型
        
        Args:
            server_name: 服务器名称
            
        Returns:
            bool: 是否处理成功
        """
        try:
            # 获取未处理的反馈
            feedback_history = self.feedback_service.prioritize_feedback_queue(server_name)
            unprocessed_feedback = [
                f for f in feedback_history 
                if not f.get('processed', False)
            ]
            
            if not unprocessed_feedback:
                logger.info("没有需要处理的反馈")
                return True
            
            # 处理每个反馈
            for feedback in unprocessed_feedback:
                reward = self.convert_feedback_to_reward(feedback)
                
                # 构建状态（简化示例，实际应用中需要更复杂的状态表示）
                state = {
                    "feedback_type": feedback.get("feedback_type", "general"),
                    "priority": feedback.get("priority", "medium"),
                    "time_of_day": time.strftime("%H", time.localtime(feedback.get("timestamp", time.time())))
                }
                
                # 根据反馈类型选择动作
                action = self._map_feedback_to_action(feedback)
                
                # 构建下一状态（简化示例）
                next_state = state.copy()
                next_state["processed"] = True
                
                # 更新Q值
                self.rl_service.update_q_value(state, action, reward, next_state)
                
                logger.info(f"处理反馈: ID={feedback.get('id', 'unknown')}, 奖励={reward}")
            
            return True
        except Exception as e:
            logger.error(f"批量处理反馈失败: {e}")
            return False
    
    def _map_feedback_to_action(self, feedback: Dict[str, Any]) -> str:
        """
        将反馈映射到具体动作
        
        Args:
            feedback: 用户反馈
            
        Returns:
            str: 动作名称
        """
        feedback_type = feedback.get("feedback_type", "general")
        priority = feedback.get("priority", "medium")
        
        # 根据反馈类型和优先级映射到动作
        action_mapping = {
            ("error", "critical"): "enable_multimodal",
            ("error", "high"): "use_multi_agent_workflow",
            ("performance", "high"): "adjust_timeout",
            ("performance", "critical"): "decrease_iterations",
            ("suggestion", "high"): "increase_iterations",
            ("security", "critical"): "use_optimized_workflow"
        }
        
        return action_mapping.get((feedback_type, priority), "use_optimized_workflow")
    
    def get_learning_stats(self) -> Dict[str, Any]:
        """
        获取学习统计信息
        
        Returns:
            Dict[str, Any]: 统计信息
        """
        return self.rl_service.get_policy_stats()


# 创建服务实例
feedback_driven_rl_service = FeedbackDrivenRLService()

def get_feedback_driven_rl_service() -> FeedbackDrivenRLService:
    """获取反馈驱动的强化学习服务实例"""
    return feedback_driven_rl_service